// pages/user/user-order/user-order.js
import { request, requestLoading } from '../../../utils/network.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperCur: 0, // 当前所在滑块的 index
    curstatus: '2', // 状态
    winHeight: 0, // 页面高度
    winWidth: 0, // 屏幕可使用宽度
    page: 1, // 参数
    pageSize: 20, // 每页数据的大小
    hasMoreData: false, // 是否还有更多
    completelist: [
      // {
      //   distributionBillId: "1",
      //   orderId: "20180202102202865552",
      //   distributionStatus: "待配货",
      //   distributeTime: "2018-02-02 10:58:39",
      //   completeTime: null,
      //   supplierCompanyName: "小刀公司",
      //   supplierLinkman: "供应商01",
      //   supplierPhone: "15067144845",
      //   brandName: "大刀",
      //   goodsName: "屠龙宝刀",
      //   goodsBn: null,
      //   productBn: null,
      //   standard: null,
      //   supplyNum: 10
      // },
      // {
      //   distributionBillId: "1",
      //   orderId: "20180202102202865552",
      //   distributionStatus: "待配货",
      //   distributeTime: "2018-02-02 10:58:39",
      //   completeTime: null,
      //   supplierCompanyName: "小刀公司",
      //   supplierLinkman: "供应商01",
      //   supplierPhone: "15067144845",
      //   brandName: "大刀",
      //   goodsName: "屠龙宝刀",
      //   goodsBn: null,
      //   productBn: null,
      //   standard: null,
      //   supplyNum: 10
      // },
      // {
      //   distributionBillId: "1",
      //   orderId: "20180202102202865552",
      //   distributionStatus: "待配货",
      //   distributeTime: "2018-02-02 10:58:39",
      //   completeTime: null,
      //   supplierCompanyName: "小刀公司",
      //   supplierLinkman: "供应商01",
      //   supplierPhone: "15067144845",
      //   brandName: "大刀",
      //   goodsName: "屠龙宝刀",
      //   goodsBn: null,
      //   productBn: null,
      //   standard: null,
      //   supplyNum: 10
      // }
    ],
    
    notCompletelist:[
      // {
      //   distributionBillId: "1",
      //   orderId: "20180202102202865552",
      //   distributionStatus: "待配货",
      //   distributeTime: "2018-02-02 10:58:39",
      //   completeTime: null,
      //   supplierCompanyName: "小刀公司",
      //   supplierLinkman: "供应商01",
      //   supplierPhone: "15067144845",
      //   brandName: "大刀",
      //   goodsName: "屠龙宝刀",
      //   goodsBn: null,
      //   productBn: null,
      //   standard: null,
      //   supplyNum: 10
      // },
      // {
      //   distributionBillId: "1",
      //   orderId: "20180202102202865552",
      //   distributionStatus: "待配货",
      //   distributeTime: "2018-02-02 10:58:39",
      //   completeTime: null,
      //   supplierCompanyName: "小刀公司",
      //   supplierLinkman: "供应商01",
      //   supplierPhone: "15067144845",
      //   brandName: "大刀",
      //   goodsName: "屠龙宝刀",
      //   goodsBn: null,
      //   productBn: null,
      //   standard: null,
      //   supplyNum: 10
      // },
      // {
      //   distributionBillId: "1",
      //   orderId: "20180202102202865552",
      //   distributionStatus: "待配货",
      //   distributeTime: "2018-02-02 10:58:39",
      //   completeTime: null,
      //   supplierCompanyName: "小刀公司",
      //   supplierLinkman: "供应商01",
      //   supplierPhone: "15067144845",
      //   brandName: "大刀",
      //   goodsName: "屠龙宝刀",
      //   goodsBn: null,
      //   productBn: null,
      //   standard: null,
      //   supplyNum: 10
      // }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    wx.getSystemInfo({
      success: function(res){
        that.setData({
          winHeight: res.windowHeight,
          winWidth: res.windowWidth
        })
      }
    })
    that.fetchOrderList(that, '数据加载中') // 获取list
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let that = this
    let curstatus = this.data.curstatus
    this.data.page = 1
    this.fetchOrderList(that, '正在刷新数据')
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom (){
    let that = this;
    if (that.data.hasMoreData) {
      that.fetchOrderList(that,'加载更多数据')
    } else {
      wx.showToast({
        title: '没有更多数据',
        icon: 'none'
      })
    }
  },
  /** 
   * 点击tab切换 
   */
  swichNav: function (e) {
    let that = this;
    let curstatus = e.target.dataset.status
    let index = e.target.dataset.index
    if (this.data.swiperCur === index) {
      return false;
    }
    that.setData({
      swiperCur: parseInt(index)
    })
  },
  swiperChange: function (e) {
    let that = this;
    let status
    e.detail.current === 0 ? status = '2' : status = '3'
    that.data.curstatus = status
    that.setData({
      swiperCur: e.detail.current
    })
   
    that.fetchOrderList(that, '加载信息...') 
  },
  // 获取配送单 ping 是否需要拼接
  fetchOrderList (that, message) {
    let status = this.data.curstatus
    let data ={
      distributionStatus: status,
      page: that.data.page,
      number: 20
    }
    requestLoading(
      '/distributor/myBillList',
      data,
      message,
      function success(res) {
        let completelist = that.data.completelist
        let notCompletelist = that.data.notCompletelist
        
        let list = res.data.list
        if (that.data.page === 1) {
          completelist = []
          notCompletelist = []
        }
        if (list.length < that.data.pageSize) {
          if (status === '2'){
            that.setData({
              completelist: completelist.concat(list),
              hasMoreData: false
            })
          }
          if (status === '3') {
            that.setData({
              notCompletelist: notCompletelist.concat(list),
              hasMoreData: false
            })
          }
        }else {
          if (current === 2) {
            that.setData({
              completelist: comlist.concat(list),
              hasMoreData: true,
              page: that.data.page + 1
            })
          }
          if (current === 3) {
            that.setData({
              notCompletelist: comlist.concat(list),
              hasMoreData: true,
              page: that.data.page + 1
            })
          }
        }
      },
      function fail() {
        wx.showToast({
          title: '加载数据失败',
          icon: 'none'
        })
      }
    )
  }
})